limsEquipmentCertificateUser.editPage = {

    getGridParam : function() {
        var _this = this;
        return {
            type : 'grid',
            id : 'lims-equipment-certificate-user-edit-list-grid',
            url : limsEquipmentCertificateUser.baseUrl + '/queries/raw',
            service : 'limsEquipmentCertificateUserServiceImpl',
            dbTable : 'T_LIMS_EQUIPMENT_CERTIFICATE_USER',
            columns : limsEquipmentCertificateUser.getEditListColumns(),
            page:true,
            onLoadSuccess: function (data) {
                if (!Gikam.isEmpty(data)) {
                    this.activeRowByIndex(0)
                } else {
                    _this.refreshTabs(null)
                }
            },
            deleteFormatter : function(row) {
                return row.personCode ;
            },
            onRowActive: function (index, row) {
                _this.refreshTabs(row)
            },
            onCellClick : function(field, row) {
                field === 'personCode' && _this.loadDetailPage(row.id);
            },
            toolbar : [ {
                type : 'button',
                text : 'GIKAM.BUTTON.INSERT',
                icon : 'add',
                onClick : function() {
                    Gikam.preInsert({
                        modalTitle : 'BASEMODULE.RM.LSIEQUIPMENTCERTIFICATES.MODAL.ADD',
                        displayReadonly: false,
                        //fields : limsEquipmentCertificateUser.getBaseInfoFormFields(),
                        fields: [{
                            field : 'personCode',//用户名
                            title : 'T_LIMS_PERSON.PERSONCODE',
                            type : 'choose',
                            search : true,
                            single : true,
                            category : 'personChoose',
                            preInsert : true,
                            /*onBeforeChoose: function () {
                                return {
                                    target_filter: encodeURI(JSON.stringify({
                                        type: 'unassigned',
                                        targetTable: 'T_LIMS_EQUIPMENT_CERTIFICATE_USER',
                                        targetMatchColumn: 'personId',
                                        thisMatchColumn: 'id'
                                    })),
                                    createdByOrgId_SEQ : workspace.user.ext$.loginorgid
                                }
                            },*/
                            onAfterChoose : function(rows) {
                                if (!Gikam.isEmpty(rows)) {
                                    this.setData({
                                        personId : rows[0].id,
                                        personName : rows[0].personName,
                                        personCode : rows[0].personCode,
                                        initials : rows[0].initials,
                                    });
                                }
                            },
                            validators : [ 'strLength[0,33]' ]

                        },  {
                            field : 'personName',
                            title : 'T_LIMS_PERSON.PERSONNAME',
                            preInsert : true,
                            readonly: true,
                        }, {
                            field : 'initials',//姓名缩写
                            title : 'T_LIMS_PERSON.INITIALS',
                            preInsert : true,
                            readonly: true,
                        }],
                        url : limsEquipmentCertificateUser.baseUrl
                    }).done(function(id) {
                        Gikam.getComp('lims-equipment-certificate-user-edit-list-grid').refresh();
                    });
                }
            }, {
                type : 'button',
                text : 'GIKAM.BUTTON.DELETE',
                icon : 'remove-row',
                onClick : function() {
                    Gikam.getComp('lims-equipment-certificate-user-edit-list-grid').deleteRows(limsEquipmentCertificateUser.baseUrl);
                }
            }, {
                type : 'button',
                text : 'GIKAM.WORKFLOW.BUTTON.SUBMIT',
                icon : 'submit', color: 'success',
                onClick : function() {
                    _this.submit();
                }
            } ]
        }
    },

    refreshTabs: function (row) {
        let _this = this
        Gikam.getAsyncComp(_this.idManager.auditRecordGrid).done(function (comp) {
            if (row && row.id) {
                comp.refresh({
                    url: IFM_CONTEXT + '/secure/core/module/sys/bpmn-comments/queries?table=T_LIMS_METHOD_CERTIFICATE_USER&id=' + row.id,
                })
            } else {
                comp.refresh({
                    url: IFM_CONTEXT + '/secure/core/module/sys/bpmn-comments/queries?table=T_LIMS_METHOD_CERTIFICATE_USER&id=' + 0
                })
            }
        })
        Gikam.getAsyncComp(_this.idManager.certificateGrid).done(function (comp) {
            if (row && row.id) {
                comp.refresh({
                    url : limsEquipmentCertificate.baseUrl + '/queries/raw',
                    requestData : {
                        personId_EQ : row.id
                    },
                })
            } else {
                comp.cleanData()
            }
        })
    },

    submit : function() {
        var grid = Gikam.getComp('lims-equipment-certificate-user-edit-list-grid');
        Gikam.create('workflow').submit({
            data : grid.getSelections(),
            pageObject : limsEquipmentCertificateUser
        }).done(function() {
            grid.refresh();
        });
    },

    loadDetailPage : function(id) {
        workspace.window.load(limsEquipmentCertificateUser.baseUrl + '/page/detail' + Gikam.param({
            id : id,
            readonly:1
        }));
    },
    getBaseInfoTab: function () {
        var _this = this
        return {
            type: 'tab',
            panels: [{
                title: 'BASEMODULE.COREDATA.METHODS.TAB.BASE_INFO',
                items: [_this.getAttachmantStruct()]
            }, {
                title: 'T_CORE_FILE.OPERATE.OPERATION.RECORD',
                items: [_this.getAuditRecordGrid()]
            }]
        }
    },

    getAttachmantStruct: function () {
        let _this = this
        return  {
            type : 'grid',
            id : _this.idManager.certificateGrid,
            page:true,
            service : 'limsEquipmentCertificateServiceImpl',
            dbTable : 'T_LIMS_EQUIPMENT_CERTIFICATE',
            columns : limsEquipmentCertificate.getEditListColumns(),
            onBeforeUpdate: function (row, keys) {
                var actRow = this.getActivedRow()
                if (keys.includes('certdate') && row.certdate && row.certdate !== '') {
                    if (actRow.expdate && actRow.expdate !== '') {
                        var certdate = new Date(row.certdate)
                        var expdate = new Date(actRow.expdate)
                        if (certdate > expdate) {
                            Gikam.alert(Gikam.propI18N('T_LIMS_METHOD.METHODTYPE.CERTDATE.TIP'))
                            this.setData({
                                certdate: '',
                                id: actRow.id,
                                index: actRow.index
                            }, false)
                            this.refreshRowById(actRow.id)
                            return false
                        }
                    }
                }
                if (keys.includes('expdate') && row.expdate && row.expdate !== '') {
                    if (actRow.certdate && actRow.certdate !== '') {
                        var certdate = new Date(actRow.certdate)
                        var expdate = new Date(row.expdate)
                        if (certdate > expdate) {
                            Gikam.alert(Gikam.propI18N('T_LIMS_METHOD.METHODTYPE.CERTDATE.TIP'))
                            this.setData({
                                expdate: '',
                                id: actRow.id,
                                index: actRow.index
                            }, false)
                            this.refreshRowById(actRow.id)
                            return false
                        }
                    }
                }
            },
            toolbar : _this.getBtnToolbarParam()
        }
    },

    getBtnToolbarParam : function() {
        var _this = this;
        return [ {
            type : 'button',
            text : 'GIKAM.BUTTON.INSERT',
            icon : 'add',
            onClick : function() {
                var activedRow = Gikam.getComp('lims-equipment-certificate-user-edit-list-grid').getActivedRow();
                if (Gikam.isEmpty(activedRow)) {
                    Gikam.alert('GIKAM.TIP.CHOOSE_AT_LEAST_ONE_ITEM');
                    return;
                }
                console.log(activedRow)
                var grid = Gikam.getComp('lims-equipment-certificate-edit-list-grid');
                var selections = grid.getData();
                var eqIds  = '';
                if(!Gikam.isEmpty(selections)){
                    eqIds =selections.map(function (item) {
                        return item.eqId;
                    }).join(",");
                }

                Gikam.create('modal', {
                    title: Gikam.choose.getConfig('equipment-multi-choose').title,
                    url: IFM_CONTEXT + Gikam.choose.getConfig('equipment-multi-choose').url + Gikam.param({
                        eqIds: eqIds
                    }),
                    onAfterClose: function (rows) {
                        console.log(rows)
                        if (Gikam.isEmpty(rows)) {
                            return;
                        }
                        // 拼接人员ID和人员name
                        var eqIds = '';
                        Gikam.each(rows, function (index, item) {
                            eqIds += item.id + ',';

                        });
                        console.log(eqIds)

                        Gikam.preInsert({
                            modalTitle : 'BASEMODULE.RM.LSIEQUIPMENTCERTIFICATES.MODAL.ADD',
                            url : limsEquipmentCertificate.baseUrl,
                            param : {
                                personId : activedRow.id,
                                eqIds : eqIds
                            },
                        }).done(function(id) {
                            Gikam.getComp('lims-equipment-certificate-edit-list-grid').refresh();
                        });
                    }
                });

            }
        }, {
            type : 'button',
            text : 'GIKAM.BUTTON.DELETE',
            icon : 'remove-row',
            onClick : function() {
                Gikam.getComp('lims-equipment-certificate-edit-list-grid').deleteRows(limsEquipmentCertificate.baseUrl);
            }
        }, {
            type : 'button',
            text : 'Date OF AUTHORIZATION',//授权日期
            icon : 'edit',
            onClick : function() {
                var grid = Gikam.getComp('lims-equipment-certificate-edit-list-grid');
                var selections = grid.getSelections();
                if (Gikam.isEmpty(selections)) {
                    Gikam.alert('GIKAM.TIP.CHOOSE_AT_LEAST_ONE_ITEM');
                    return;
                }
                var model = Gikam.create('modal', {
                    id:'lims-equipment-certificate-edit-date',
                    title: 'Date OF AUTHORIZATION',
                    height: '200',
                    width: '400',
                });
                Gikam.create('layout', {
                    renderTo: model.window.$dom,
                    center: {
                        items: [{
                            type: 'form',
                            id: 'lims-equipment-certificate-edit-date-form',
                            fields: limsEquipmentCertificate.getBaseInfoFormFields(),
                            columns: 1,
                            titleWidth: 80
                        }, {
                            type: 'btnToolbar',
                            items: [{
                                type: 'button',
                                text: 'GIKAM.BUTTON.CONFIRM',
                                class: 'blue',
                                icon: 'save',
                                color: 'white',
                                onClick: function () {
                                    var form = Gikam.getComp('lims-equipment-certificate-edit-date-form');
                                    if (form.validate()) {
                                        workspace.window.showMask()
                                        var data = form.getData()
                                        var certdate = new Date(data.certdate)
                                        var expdate = new Date(data.expdate)
                                        if (certdate > expdate) {
                                            Gikam.alert(Gikam.propI18N('T_LIMS_METHOD.METHODTYPE.CERTDATE.TIP'))
                                            Gikam.getLastModal().close();
                                            return ;
                                        }

                                        data.id=selections[0].id,
                                            data.ids=selections.map(s => s.id).join(','),
                                            Gikam.each(selections, function(index, item) {
                                                item.certdate = data.certdate;
                                                item.expdate = data.expdate;
                                            });
                                        Gikam.postText(limsEquipmentCertificate.baseUrl+'/updateDate' ,
                                            Gikam.getJsonWrapper("",[null,selections])).done(function () {
                                            grid.refresh();
                                            Gikam.getLastModal().close();
                                        }).always(function () {
                                            Gikam.getLastModal().close();
                                        });
                                    }

                                }
                            }, {
                                type: 'button',
                                text: 'GIKAM.BUTTON.CANCEL',
                                icon: 'back',
                                onClick: function () {
                                    Gikam.getLastModal().close();
                                }
                            }]
                        }]
                    }
                })

            }
        }]
    },

    getAuditRecordGrid: function () {
        let _this = this
        return {
            type: 'grid',
            id: _this.idManager.auditRecordGrid,
            columns: limsEquipmentCertificate.getFlowListColumns()
        }
    },

    idManager: {
        certificateGrid: 'lims-equipment-certificate-edit-list-grid',
        auditRecordGrid: 'lims-equipment-certificate-edit-list-grid' + '-record',
    },

    create : function() {
        var _this = this;
        Gikam.create('layout', {
            id : 'lims-equipment-certificate-user-edit-list-layout',
            renderTo : workspace.window.$dom,
            /*north: {
                height: '55%',
                items: [this.getGridParam()]
            },*/
            center: {
                items: [this.getGridParam()]
            }
        });
    },

    init : function() {
        this.create();
    }
};
